import axios from 'axios'
import store from '../store'
import router from '../router'
import iView from "view-design";

export const baseUrl = 'https://www.ddandang.top:9000';
//export const baseUrl = 'http://localhost:9000';
export function request(config) {

  iView.LoadingBar.start();
  iView.Spin.show();

  const instance = axios.create({
    baseURL: baseUrl,
    timeout: 5000,
    headers: {
      'Authentication-Token': store.state.token
    }
  })

  //拦截器
  instance.interceptors.response.use(res => {
    // setTimeout(() => {
    //
    // }, 2000);
    iView.Spin.hide();
    if(res.data.code !== 2000) {
      iView.LoadingBar.error();
    }else {
      iView.LoadingBar.finish();
    }
    return res.data;
  }, error => {
    //登录失效
    if(error.response.status === 520) {
      store.commit('changeIndex','1');
      //删除数据
      store.commit('resetUser');
      //跳回首页
      router.push({path: '/'})
    }
    iView.LoadingBar.error();
    return error;
  })


  return instance(config);

}

